<template>
  <div style="width:100%;overflow-x:hidden">
    <weui-input :title="oldUrl" :list="input_list_0" more_label="" agreement_name="" :tips="null">
    </weui-input>
    <!--<rater slot="after-title" :value.sync="star"></rater>-->
    <vux-divider>
      word
    </vux-divider>
    <div style="display:flex">
      <div style="flex:2">
        <weui-input :list="input_list_1" title="" more_label="" agreement_name="" :tips="null">
        </weui-input>
      </div>
      <div style="flex:1">
        <weui-input :list="input_list_2" title="" more_label="" agreement_name="" :tips="null">
        </weui-input>
      </div>
    </div>
    <weui-button @on-click="onAddClick">
      添加
    </weui-button>
    <weui-button type="primary" @on-click="onSubmitClick">
      提交
    </weui-button>
  </div>
</template>

<script>
  import WeuiInput from '../vue2componentSet/src/components/WeuiInput'
  import WeuiButton from '../vue2componentSet/src/components/WeuiButton'
  import VuxDivider from '../components/VuxDivider'
  import global from '../lib/global'
  import axios from 'axios'

  export default {
    components: {
      WeuiInput,
      WeuiButton,
      VuxDivider
    },
    data() {
      return {
        oldUrl: '',
        star: 0,
        input_list_0: [{
          label: 'url',
        }, {
          label: 'title'
        }, {
          label: 'p'
        }],
        input_list_1: [],
        input_list_2: []
      }
    },
    mounted() {
      this.$store.commit('setTitle', 'edit')
      var start = location.hash.indexOf('?')
      if (start == -1) {
        this.$router.goback()
        return
      }
      this.oldUrl = decodeURIComponent(location.hash.substring(start + 5))

      if (global.is_debug) {
        this.$set(this.input_list_0[0], 'value', 'http://www.baidu.com')
        this.$set(this.input_list_0[1], 'value', '百度')
        this.$set(this.input_list_0[2], 'value', '1')
        this.input_list_1.push({
          value: '百度'
        })
        this.input_list_2.push({
          value: '1'
        })
        return
      }
      axios.post('/getUrlInfo.do', {
        url: this.oldUrl
      }).then(result => {
        var resObj = result.data
        if (resObj.result == -1) {
          this.$store.commit('toast', {
            type: 'warn',
            content: resObj.msg
          })
          this.$router.goback()
        } else if (resObj.result == 0) {
          this.$store.commit('login', '')
          this.$store.commit('toast', {
            type: 'warn',
            content: '请登录'
          })
          this.$router.redirect('/login?redirectTo=' + encodeURIComponent(location.hash.substring(1)))
        } else {
          this.$set(this.input_list_0[0], 'value', resObj.url)
          this.$set(this.input_list_0[1], 'value', resObj.title)
          this.$set(this.input_list_0[2], 'value', resObj.p + '')
          resObj.wordList.forEach(word => {
            this.input_list_1.push({
              value: word.word
            })
            this.input_list_2.push({
              value: word.p + ''
            })
          })
          this.star = resObj.star
        }
      }, (response) => {
        console.log(response)
      })
    },
    methods: {
      onSubmitClick() {
        if (!this.input_list_0[0].value) {
          this.$store.commit('toast', {
            type: 'warn',
            content: '请输入url'
          })
          return
        }
        if (isNaN(parseFloat(this.input_list_0[2].value))) {
          this.$store.commit('toast', {
            type: 'warn',
            content: '请输入有效的P'
          })
          return
        }
        this.$store.commit('loading', {
          is_show: true
        })
        const wordList = []
        for (var i = 0; i < this.input_list_1.length; i++) {
          if (!this.input_list_1[i].value || isNaN(parseFloat(this.input_list_2[i].value))) {
            continue
          }
          wordList.push({
            word: this.input_list_1[i].value,
            p: this.input_list_2[i].value
          })
        }
        axios.post('/updateUrlInfo.do', {
          oldUrl: this.oldUrl,
          url: this.input_list_0[0].value,
          title: this.input_list_0[1].value,
          p: this.input_list_0[2].value,
          wordList: wordList,
          star: this.star
        }).then(result => {
          this.$store.commit('loading', {
            is_show: false
          })
          var resObj = result.data
          if (resObj.result == 1) {
            this.$store.commit('toast', {
              type: 'success',
              content: '成功'
            })
          } else if (resObj.result == 0) {
            this.$store.commit('toast', {
              type: 'warn',
              content: '请先登录'
            })
            this.$router.push('/login?redirectTo=' + encodeURIComponent(location.hash.substring(1)))
          } else {
            this.$store.commit('toast', {
              type: 'warn',
              content: resObj.msg
            })
          }
        }, (response) => {
          this.$store.commit('loading', {
            is_show: false
          })
          console.log(response)
        })
      },
      onAddClick() {
        this.input_list_1.push({
          value: ''
        })
        this.input_list_2.push({
          value: ''
        })
      }
    }
  }
</script>
